package com.wanmait.happyheating.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.FaultType;
import com.wanmait.happyheating.pojo.MaterialsUse;
import com.wanmait.happyheating.pojo.Ordersinfo;
import com.wanmait.happyheating.pojo.Workers;
import com.wanmait.happyheating.service.MaterialsUseService;
import com.wanmait.happyheating.service.OrdersinfoService;
import com.wanmait.happyheating.service.WorkersService;
import com.wanmait.happyheating.util.PageHelperUtils;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/userViewOrder")
public class UserViewOrderController {

    @Resource OrdersinfoService ordersinfoService;

    @Resource
    WorkersService workersService;

    @Resource
    MaterialsUseService materialsUseService;

    /**
     * 查找该用户所有订单，并根据下单时间排序。
     */
    @PostMapping("allInfo")
    public Result allInfo(@RequestBody Integer pageNum, Integer pageSize, Integer userId){
        if(pageNum==null)
        {
            pageNum=1;
        }
        if (userId==0){
            PageInfo<Ordersinfo> ordersinfoPageInfo=ordersinfoService.findUserOrderInfoByPage(pageNum,pageSize,userId);
            return Result.success("查找成功",ordersinfoPageInfo);
        }
        PageInfo<Ordersinfo>ordersinfoPageInfo=ordersinfoService.findUserOrderInfoByPage(pageNum,pageSize,userId);

        return Result.success(ordersinfoPageInfo);
    }

    /**
     * 根据订单中的workerID查找worker详细信息
     */
    @PostMapping("workerInfo")
    public Result workerInfo(@RequestBody Integer workerId){
        LambdaQueryWrapper<Workers>workersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        workersLambdaQueryWrapper.eq(Workers::getId,workerId);
        return Result.success(workersService.getOne(workersLambdaQueryWrapper));
    }
    /**
     * 根据订单中的订单ID查找耗材明细
     */
    @PostMapping("materialsIndo")
    public Result materialsIndo(@RequestBody Integer ordersinfoId){
        LambdaQueryWrapper<MaterialsUse>materialsUseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        materialsUseLambdaQueryWrapper.eq(MaterialsUse::getOrdersinfoId,ordersinfoId);
        return Result.success(materialsUseService.list(materialsUseLambdaQueryWrapper));
    }





}
